//
//  ExclusiveArtistViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ExclusiveArtistViewController.h"
#import "ArtistCollectionViewCell.h"
#import <AFNetworking.h>
#import "ArtistModel.h"
#import "SongerListViewController.h"
#import "OtherSongerListViewController.h"
#import "YTCollectionViewCell.h"

@interface ExclusiveArtistViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *array;


@end

@implementation ExclusiveArtistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.array = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createData];
    [self createView];
    
}

- (void)createData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.dongting.com/song/singer_classify/items??app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dic in [responseObject valueForKey:@"data"]) {
            ArtistModel *model = [ArtistModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        [self.collectionView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)createView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(20, 10, 10, 10);
    layout.itemSize = CGSizeMake(WIDTH - 20, 50);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[YTCollectionViewCell class] forCellWithReuseIdentifier:@"AAA"];
    [self.view addSubview:self.collectionView];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
//    NSLog(@"%ld",self.array.count);
    return self.array.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return [[self.array[section] singerClassifyGroup] count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YTCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AAA" forIndexPath:indexPath];
//    ArtistCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AAA" forIndexPath:indexPath];
    
//    NSLog(@"%@",self.array[indexPath.section]);
    ArtistModel *Artmodel = self.array[indexPath.section];
    SingerClassifyGroupModel *model = Artmodel.singerClassifyGroup[indexPath.row];
//    NSLog(@"%@",model);
//   SingerClassifyGroupModel *GROU = [[SingerClassifyGroupModel alloc] init];
    cell.singerModel = model;
//    NSLog(@"%@",GROU.itemName);
//    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    SingerClassifyGroupModel *model = [[self.array[indexPath.section] singerClassifyGroup] objectAtIndex:indexPath.row];
    if (indexPath.section == 0) {
        SongerListViewController *songerListVC = [SongerListViewController new];
        songerListVC.songerId = model.itemId;
        [self.navigationController pushViewController:songerListVC animated:YES];
    } else {
        OtherSongerListViewController *otherSongerListVC = [OtherSongerListViewController new];
        otherSongerListVC.songerId = model.itemId;
        [self.navigationController pushViewController:otherSongerListVC animated:YES];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
